package com.yuw.controller;

import com.yuw.bean.UserInfoBean;
import com.yuw.service.IUserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Controller
// @CrossOrigin(origins = {"http://localhost:63343"},maxAge = 3000)
@RequestMapping("/cross") // 整个controller的映射路径，后面需要添加方法的应属于路径才是正题的访问路径
public class UserCorsController {
    // 依赖注入
    @Resource
    private IUserService userService;

    //@CrossOrigin
    @RequestMapping(value = {"/queryByParams"}, method = RequestMethod.POST)
    public ResponseEntity<List<UserInfoBean>> queryUserInfoByParams(UserInfoBean userInfoBean) {

        // ajax请求返回的是ajax内容串；
        // 普通的请求返回的是视图名【区别】
        List<UserInfoBean> userInfoBeans = userService.selectUserinfoByParams(userInfoBean);
        // 直接返回结果集即可，springmvc框架会自动将结果集转换为json字符串，但是需要在springmvc的配置文件中进行相关配置
        // return ResponseEntity.status(HttpStatus.OK).body(userInfoBeans);
        //return ResponseEntity.ok(userInfoBeans);
        return new ResponseEntity<>(userInfoBeans, HttpStatus.OK);
    }
}

